package cn.iocoder.yudao.module.system.service.investment;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.framework.web.core.util.WebFrameworkUtils;
import cn.iocoder.yudao.module.infra.api.file.FileApi;
import cn.iocoder.yudao.module.system.controller.admin.investment.vo.ParticipationReqVO;
import cn.iocoder.yudao.module.system.controller.admin.investment.vo.ParticipationRespVO;
import cn.iocoder.yudao.module.system.controller.admin.investment.vo.ParticipationUpdateReqVO;
import cn.iocoder.yudao.module.system.convert.investment.ParticipationConvertToResp;
import cn.iocoder.yudao.module.system.dal.dataobject.investment.SystemParticipation;
import cn.iocoder.yudao.module.system.dal.mysql.investment.ParticipationMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.*;

/**
 * @author 梁富贵
 * @Date 2024/10/11
 **/
@Slf4j
@Service
public class ParticipationServiceImpl implements ParticipationService{

    @Resource
    private ParticipationMapper participationMapper;

    /**
     * 分页查询参与合作招商者
     * @param reqVO
     * @return
     */
    @Override
    public PageResult<ParticipationRespVO> getParticipationPage(ParticipationReqVO reqVO) {
        LambdaQueryWrapperX<SystemParticipation> query = new LambdaQueryWrapperX<>();
        query.likeIfPresent(SystemParticipation::getCompanyName, reqVO.getCompanyName())
                .betweenIfPresent(SystemParticipation::getApplicationDate, reqVO.getApplicationDate()[0], reqVO.getApplicationDate()[1])
                .eqIfPresent(SystemParticipation::getParticipationId, reqVO.getParticipationId())
                .eqIfPresent(SystemParticipation::getId, reqVO.getId());

        PageResult<SystemParticipation> result = participationMapper.selectPage(reqVO, query);
        return new PageResult<>(ParticipationConvertToResp.toResp(result.getList()), result.getTotal());
    }

    /**
     * 创建参与招商者
     * @param systemParticipation
     * @return
     */
    @Override
    public int createParticipation(SystemParticipation systemParticipation) {
        return participationMapper.insert(systemParticipation);
    }

    /**
     * 删除参与招商者
     * @param participationId
     * @return
     */
    @Override
    public int deleteParticipation(Integer participationId) {
        return participationMapper.deleteById(participationId);
    }

    /**
     * 更新参与招商者信息
     * @param participationUpdateReqVO
     * @return
     */
    @Override
    public int updateParticipation(ParticipationUpdateReqVO participationUpdateReqVO, String requestMappingPath) throws IOException {
        SystemParticipation systemParticipation = BeanUtils.toBean(participationUpdateReqVO, SystemParticipation.class);

        systemParticipation.setAddress(String.join(",", Arrays.asList(participationUpdateReqVO.getAddress())));

        LambdaQueryWrapperX<SystemParticipation> query = new LambdaQueryWrapperX<>();
        query.eq(SystemParticipation::getId,systemParticipation.getId());
        SystemParticipation oldParticipation = participationMapper.selectOne(query);
        if(Objects.isNull(oldParticipation)){
            return -1;
        }

        return participationMapper.updateParticipation(systemParticipation);
    }

    /**
     * 通过用户id查询已申请项目
     * @param reqVO
     * @return
     */
    @Override
    public PageResult<ParticipationRespVO> getParticipationListByUserId(ParticipationReqVO reqVO) {
        if(Objects.isNull(WebFrameworkUtils.getLoginUserId())){
            return null;
        }
        LambdaQueryWrapperX<SystemParticipation> query = new LambdaQueryWrapperX<>();
        query.eqIfPresent(SystemParticipation::getParticipationId,reqVO.getParticipationId());
        query.likeIfPresent(SystemParticipation::getCompanyName, reqVO.getCompanyName());
        if(reqVO.getApplicationDate() != null && reqVO.getApplicationDate().length == 2){
            query.betweenIfPresent(SystemParticipation::getApplicationDate, reqVO.getApplicationDate()[0], reqVO.getApplicationDate()[1]);

        }
        query.eqIfPresent(SystemParticipation::getId,reqVO.getId());
        query.eq(SystemParticipation::getInvestmentId, WebFrameworkUtils.getLoginUserId());

        PageResult<SystemParticipation> result = participationMapper.selectPage(reqVO, query);
        return new PageResult<>(ParticipationConvertToResp.toResp(result.getList()), result.getTotal());
    }

    /**
     * 通过申请
     * @param reqVO
     */
    @Override
    public Integer passingParticipationList(List<Integer> reqVO) {
        return participationMapper.passingParticipationApplication(reqVO);
    }

    /**
     * 驳回申请
     * @param reqVO
     * @return
     */
    @Override
    public Integer rejectParticipationApplications(List<Integer> reqVO) {
        return participationMapper.rejectedParticipationApplication(reqVO);
    }

}
